<template>
    <div>
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="userName" label="用户名" placeholder="用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]" />
                <van-field v-model="password" type="password" name="passWord" label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    提交
                </van-button>
            </div>
        </van-form>
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { showToast } from 'vant';
//引入路由
import {useRouter} from 'vue-router'
//实例化
const router=useRouter()
const username = ref('');
const password = ref('');
const onSubmit = ({ userName, passWord }) => {
    if (userName === 'admin' && passWord === '123456') {
        //存储在本地浏览器
        localStorage.setItem('token', 'asddwetcvb')
        //跳转至首页
        router.push('/')
        showToast('登录成功');
    } else {
        showToast('账号密码错误');
        username.value = ''
        password.value = ''
    }
};

</script>

<style lang="scss" scoped></style>